layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    var tableIns = table.render({
        elem: '#roleList',
        url : '/main/getRoleList',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 10,
        limits : [10,15,20,20,25],
        id : "roleListTable",
        cols : [[
            {field: 'roleName', title: '角色名称'},
            {field: 'compName', title: '公司', align:'center', sort: true},
            {field: 'percent', title: '分成', align:'center'},
            {field: 'des', title: '备注', align:'center'},
            {title: '操作', width:170, templet:'#roleListBar',fixed:"right",align:"center"}
        ]]
    });
    //添加角色
    function roleAdd(){
        var index = layui.layer.open({
            title : "操作角色",
            type : 2,
            content : "/roleAdd/index",
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        });
    };
    //编辑
    function roleEdit(edit){
    	var index = layui.layer.open({
    		title : "操作角色",
    		type : 2,
    		content : "/roleEdit/index?roleId="+edit.roleId,
    		success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                	body.find(".roleId").attr("value",edit.roleId);
                    body.find(".roleName").val(edit.roleName);
                    body.find(".percent").val(edit.percent);
                    body.find(".des").val(edit.des);
                    form.render();
                };
            },
    	});
    	layui.layer.full(index);
    	//改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
    	$(window).on("resize",function(){
    		layui.layer.full(index);
    	});
    };
    $(".role_add").on("click",function(){
    	roleAdd();
    });

	    // 列表操作
	table.on('tool(roleList)', function(obj) {
		var layEvent = obj.event, data = obj.data;

		if (layEvent === 'edit') { // 编辑
			roleEdit(data);
		} else if (layEvent === 'del') { // 删除
			layer.confirm('确定删除此角色？', {
				icon : 3,
				title : '提示信息'
			}, function(index) {
				if (index) {
					$.get("/main/deleteRole?roleId=" + data.roleId, function(result) {
						if (result.success) {
							layer.open({
								title : '温馨提示',
								content : "删除成功"
							});
							tableIns.reload();
						} else {
							layer.open({
								title : '温馨提示',
								content : result.message
							});
							tableIns.reload();
						}
					})
				} else {
					tableIns.reload();
				}

			});

		}

	});
})